import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

/**
 * Created by weihuajiang on 2017/6/30.
 * stream 学习类
 */
public class StreamDemo {

    public Stream<User> fiter1(List<User> users) {
        return users.stream().filter(user -> user.getUserId() == 1L);

    }

    public static void main(String[] args) {

        List<User> users = new ArrayList<>();
        users.add(new User(1L, "11"));
        users.add(new User(1L, "11"));
        users.add(new User(2L, "12"));
        users.add(new User(3L, "13"));

        StreamDemo demo = new StreamDemo();
        try {

            // list
            List<User> userList = users.stream().filter(user -> user.getUserId() == 1L).collect(toList());
            long count = users.stream().count();
            List<User> userListLimit = users.stream().limit(1).collect(toList());
            List<User> userListSkip = users.stream().skip(2).collect(toList());
            List<User> userListPeek = users.stream()
                    .limit(3)
                    .peek(user -> System.out.println(user.getUserId()))
                    .skip(2)
                    .peek(user -> System.out.println(user.getUserId()))
                    .collect(toList());

            List<User> userListMap = users.stream()
                    .map(user -> {
                        user.setAge("11");
                        return user;
                    }).collect(toList());

            boolean la = users.stream().allMatch(user -> user.getAge().equals("121"));
            boolean lb = users.stream().noneMatch(user -> user.getAge().equals("121"));
            boolean lc = users.stream().anyMatch(user -> user.getAge().equals("11"));

            // array
            String nums[] = new String[]{"a", "b", "c", "d"};
            Stream.of(nums).filter(s -> s.equals("a")).peek(s -> System.out.println(s)).toArray();
            System.out.println("in");


        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
